﻿/*常客相关方法
 * 
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Text;
using EZCRS.HCMS.Bll;
using EZCRS.HCMS.Bll.userinfo;

namespace EZCRS.HCMS.myajax
{
    public class CardUserUtils:MainBase
    {
        ArrayList arrRecord;
        //记录数量
        int iRecordCount = 0;
        //总页数
        int iPageCount = 0;

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public ArrayList cardnouseall(int cpage, string search)
        {
            #region

            arrRecord = new ArrayList();

            string gid = BasGroupId.ToString();

            int pageindex = Convert.ToInt32(cpage);
            DataSet ds = null;
            string strwhere=" orgid="+gid+" ";

            //strwhere += " and uid is null ";

            if (search != "")
                strwhere += " and cardnumber like '%"+CFun.VerifySQL(search)+"%'";

            Bllcardinfo cardnumber = new Bllcardinfo();
            ds = cardnumber.CardNumb_List1(pageindex, strwhere);


            DataTable dt = ds.Tables[0];
            iRecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());

            iPageCount = (iRecordCount / CFun.PageSize);
            if (iRecordCount % CFun.PageSize > 0)
            {
                iPageCount++;
            }

            Blluser ALS = new Blluser();
            DataTable tableList = ALS.LevelSetUp_List(gid, "0");
         
            //组合字符串
            string cardid = "";

            StringBuilder strTable = new StringBuilder();
            #region
            strTable.Append("<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" width=\"100%\"><tr class=\"title1\" height=\"26\"><td width=\"6%\" style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrNumb+"</td><td width=\"10%\" style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrCardNo+"</td><td width=\"10%\"  style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrCardPass+"</td><td width=\"20%\" style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrCardType+"</td><td width=\"12%\" style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrSort+"</td><td width=\"6%\" style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrAdmin+"</td><td width=\"6%\" style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrAvailibaleDate+"</td><td width=\"1%\" style=\"color: white;\"><input type=\"checkbox\" id=\"cbx\" onclick=\"clientselect(this);\"/></td></tr>");
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int row = 0; row < dt.Rows.Count; row++)
                {
                    strTable.Append("<tr");
                    if (row % 2 == 0)
                    {
                        strTable.Append(" class='line-odd'");
                    }
                    else
                    {
                        strTable.Append(" class='line-even'");
                    }
                    strTable.Append(" onmouseover=\"currentcolor=this.style.backgroundColor;this.style.backgroundColor='#B9CEEA';\"");
                    strTable.Append(" onmouseout=\"this.style.backgroundColor=currentcolor;\"");

                    if (dt.Rows[row]["uid"].ToString() != "")
                        strTable.Append(" onclick=\"location.href='carduser_modi.aspx?mid=" + dt.Rows[row]["uid"].ToString() + "';\" style=\"cursor:hand;\" ");
                    else
                        cardid += "," + dt.Rows[row]["cardid"].ToString();

                    strTable.Append(">");
                    string sNo = "";
                    if (((pageindex - 1) * CFun.PageSize + row + 1) < 10)
                    {
                        sNo = "0" + Convert.ToString(row + 1);
                    }
                    else
                    {
                        sNo = Convert.ToString((pageindex - 1) * CFun.PageSize + row + 1);
                    }
                    strTable.Append("<td><div align=\"center\">");
                    strTable.Append(sNo.ToString());
                    strTable.Append("</div></td>");
                    strTable.Append("<td><div align=\"center\">");
                    strTable.Append(ds.Tables[0].Rows[row]["cardnumber"].ToString() );
                    strTable.Append("</div></td>");

                    strTable.Append("<td><div align=\"center\">");
                    strTable.Append("******");
                    strTable.Append("</div></td>");
                    strTable.Append("<td><div align=\"center\">");
                    
                    strTable.Append(userlevelname(tableList,dt.Rows[row]["cardtype"].ToString()));
                    
                    strTable.Append("</div></td>");
                    strTable.Append("<td><div align=\"center\">");
                    if (dt.Rows[row]["uid"].ToString() != "")
                        strTable.Append(""+EZCRS.HCMS.myajax.Site.StrUsed+"");
                    else
                        strTable.Append(""+EZCRS.HCMS.myajax.Site.StrNoUsed+"");
                    
                    strTable.Append("</div></td>");
                    strTable.Append("<td><div align=\"center\">");
                    strTable.Append(ds.Tables[0].Rows[row]["useadm"].ToString());
                    strTable.Append("</div></td>");
                    strTable.Append("<td><div align=\"center\">");
                    strTable.Append(ds.Tables[0].Rows[row]["usedate"].ToString());
                    strTable.Append("</div></td>");
                    strTable.Append("<td><div align=\"center\"><input type=\"checkbox\" id=\"cbx" + ds.Tables[0].Rows[row]["cardid"].ToString() + "\"");

                    if (dt.Rows[row]["uid"].ToString() != "")
                        strTable.Append(" disabled ");

                    strTable.Append("></div></td>");
                    strTable.Append("</tr>");
                }
            }
            else
            {
                strTable.Append("<tr height=\"35\" class=\"line-odd\"><td colspan=\"8\" align=\"center\">"+EZCRS.HCMS.myajax.Site.StrNonData+"</td></tr>");
            }
            strTable.Append("</table><input type=\"hidden\" id=\"hidcardid\" value=\"" + cardid + "\">");
            #endregion
            //将表格放入arraylist
            arrRecord.Add(strTable.ToString());
            //将总页数放入arraylist
            arrRecord.Add(iPageCount.ToString() == "0" ? "1" : iPageCount.ToString());
            //将总记录数放入arraylist
            arrRecord.Add(iRecordCount.ToString());

            return arrRecord;

            #endregion
        }


        private string userlevelname(DataTable dt, string lvlid)
        {
            string rstr = ".null.";
            foreach (DataRow dr in dt.Rows)
                if (dr["lvlid"].ToString() == lvlid)
                {
                    rstr = dr["lvlname"].ToString();
                    break;
                }
            return rstr;
        }

        /// <summary>
        /// 删除卡号
        /// </summary>
        /// <param name="cardid">卡号列</param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string cardnousedel(string cardid)
        {
            cardid = CFun.getidlist(cardid);

            Bllcardinfo cardnumber = new Bllcardinfo();
            if (cardnumber.CardNumb_Del(cardid))
                return "1";
            else
                return "0";
        }


        /// <summary>
        /// 常客积分历史（包括获得、消费、冻结、购买）
        /// </summary>
        /// <param name="cpage"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public ArrayList cardscorelist(int cpage, string search)
        {
            #region

            arrRecord = new ArrayList();

            string gid = BasGroupId.ToString();

            int pageindex = Convert.ToInt32(cpage);
            DataSet ds = null;
            string strwhere = " gid=" + gid + " ";

            string[] str = search.Split('|');

            if (str.Length != 5)
            {
                arrRecord.Add(".null.");
                return arrRecord;
            }

            Bllscore scorelog = new Bllscore();
            ds = scorelog.scoreloglist(gid, str[0], cpage, str[1], str[2], str[3], str[4]);

            DataTable dt = ds.Tables[0];
            iRecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());

            iPageCount = (iRecordCount / CFun.PageSize);
            if (iRecordCount % CFun.PageSize > 0)
            {
                iPageCount++;
            }

          
            //组合字符串
            string cardid = "";

            StringBuilder strTable = new StringBuilder();
            #region
            int kk = 0;
            strTable.Append("<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" width=\"100%\"><tr class=\"title1\" height=\"26\"><td width=\"6%\" style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrNumb+"</td><td width=\"10%\" style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrScoreDate+"</td><td width=\"10%\"  style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrScoreTotal+"</td><td width=\"6%\" style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrScoreType+"</td><td width=\"6%\" style=\"color: white;\">"+EZCRS.HCMS.myajax.Site.StrAvailable+"</td><td width=\"1%\" style=\"color: white;\"><input type=\"checkbox\" id=\"cbx\" onclick=\"clientselect(this);\"/></td></tr>");
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int row = 0; row < dt.Rows.Count; row++)
                {
                    strTable.Append("<tr");
                    if (row % 2 == 0)
                    {
                        strTable.Append(" class='line-odd'");
                    }
                    else
                    {
                        strTable.Append(" class='line-even'");
                    }
                    strTable.Append(" onmouseover=\"currentcolor=this.style.backgroundColor;this.style.backgroundColor='#B9CEEA';\"");
                    strTable.Append(" onmouseout=\"this.style.backgroundColor=currentcolor;\"");

                        cardid += "," + dt.Rows[row]["logid"].ToString();

                    strTable.Append(">");
                    string sNo = "";
                    if (((pageindex - 1) * CFun.PageSize + row + 1) < 10)
                    {
                        sNo = "0" + Convert.ToString(row + 1);
                    }
                    else
                    {
                        sNo = Convert.ToString((pageindex - 1) * CFun.PageSize + row + 1);
                    }
                    strTable.Append("<td><div align=\"center\">");
                    strTable.Append(sNo.ToString());
                    strTable.Append("</div></td>");
                    strTable.Append("<td><div align=\"center\">");
                    strTable.Append(ds.Tables[0].Rows[row]["scoredate"].ToString());
                    strTable.Append("</div></td>");

                    strTable.Append("<td><div align=\"center\">");
                    strTable.Append(ds.Tables[0].Rows[row]["score"].ToString());
                    strTable.Append("</div></td>");
                    strTable.Append("<td><div align=\"center\">");

                    strTable.Append(Common.scorelogsort(Convert.ToInt32(ds.Tables[0].Rows[row]["logsort"].ToString())));

                    strTable.Append("</div></td>");
                    if (ds.Tables[0].Rows[row]["valid"].ToString() == "True")
                        kk = 1;
                    else
                        kk = 0;
                    strTable.Append("<td><div align=\"center\">"+Common.BoolStatus(kk)+"</div></td>");
          
                    strTable.Append("<td><div align=\"center\"><input type=\"checkbox\" id=\"cbx" + ds.Tables[0].Rows[row]["logid"].ToString() + "\"");

                    

                    strTable.Append("></div></td>");
                    strTable.Append("</tr>");
                }
            }
            else
            {
                strTable.Append("<tr height=\"35\" class=\"line-odd\"><td colspan=\"6\" align=\"center\">"+EZCRS.HCMS.myajax.Site.StrNonData+"</td></tr>");
            }
            strTable.Append("</table><input type=\"hidden\" id=\"hidlogid\" value=\"" + cardid + "\">");
            #endregion
            //将表格放入arraylist
            arrRecord.Add(strTable.ToString());
            //将总页数放入arraylist
            arrRecord.Add(iPageCount.ToString() == "0" ? "1" : iPageCount.ToString());
            //将总记录数放入arraylist
            arrRecord.Add(iRecordCount.ToString());

            return arrRecord;

            #endregion
        }

        /// <summary>
        /// 改变积分日志状态
        /// </summary>
        /// <param name="logidlist"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string cardscorestatus(string logidlist)
        {
            #region
            logidlist = CFun.getidlist(logidlist);

            Bllscore scorelog = new Bllscore();
            if (scorelog.scorelogstatus(logidlist))
                return "1";//成功
            else
                return "0";
            #endregion
        }


        /// <summary>
        /// 处理积分购买
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="moneys"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string cardscorebuy(string uid, string moneys)
        {
            #region
            string gid = BasGroupId.ToString();
            string admid = CFun.GetCookies("cms", "n");
            Bllscore admscore = new Bllscore();
            return admscore.scorebuy(gid, uid, moneys, admid).ToString();
            #endregion
        }

    }
}
